package com.yylending.las.lending.increase;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterLoader;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.StatIDSet;
import com.yylending.las.base.BaseFragment;
import com.yylending.las.common.widgets.RedDotView;
import com.yylending.las.common.widgets.TelephoneTextView;
import com.yylending.las.lending.QuotaEvent;
import com.yylending.las.lending.outlet.OutletAppointActivity;
import com.yylending.las.lending.process.IncreaseLimitByPolicyActivity;
import com.yylending.las.lending.process.PreIncreaseLimitActivity;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.main.MainActivity;
import com.yylending.las.main.MainController;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 准备提额界面
 * Created by Pengrf on 2017/5/25.
 * Copyright  2017 www.yyfax.com. All Rights Reserved.
 */
public class PreIncreaseLimitFragment extends BaseFragment {

	private static final String TAG = PreIncreaseLimitFragment.class.getSimpleName();
	private static final int LOADER_ID_INCREASE_LIMIT = 10001;
	private static final int REQUEST_CODE_POLICY = 1230;
	static final String ARG_APPLY_STATUS = "arg_apply_status";

	Unbinder unbinder;
	@BindView(R.id.title_left_iv)
	ImageView mTitleLeftIv;
	@BindView(R.id.title_tv)
	TextView mTitleTv;
	@BindView(R.id.title_right_tv)
	TextView mTitleRightTv;
	@BindView(R.id.title_right_iv)
	ImageView mTitleRightIv;
	@BindView(R.id.layout_title_bar)
	RelativeLayout mLayoutTitleBar;
	@BindView(R.id.service_phone_tv)
	TelephoneTextView mServicePhoneTv;
	@BindView(R.id.red_dot_view)
	RedDotView mRedDotView;
	@BindView(R.id.pre_increase_policy_iv)
	ImageView mPreIncreasePolicyIv;

	MyQuotaModel.ApplyStatus mApplyStatus;

	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		getLoaderManager().initLoader(LOADER_ID_INCREASE_LIMIT, null, this);
	}

	@Override
	public void onDetach() {
		getLoaderManager().destroyLoader(LOADER_ID_INCREASE_LIMIT);
		super.onDetach();
	}

	public static BaseFragment newInstance(MyQuotaModel.ApplyStatus status) {
		BaseFragment fragment = new PreIncreaseLimitFragment();
		Bundle args = new Bundle();
		args.putParcelable(ARG_APPLY_STATUS, status);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_pre_increase_limit, container, false);
		unbinder = ButterKnife.bind(this, view);
		mApplyStatus = getArguments().getParcelable(ARG_APPLY_STATUS);
		return view;
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initView();
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	private void initView() {
		mTitleLeftIv.setImageResource(R.drawable.ic_home_help);
		mTitleTv.setText(R.string.tab_lend);
		mTitleRightIv.setImageResource(R.drawable.ic_message_white);
		mServicePhoneTv.setPageNo(Constants.PAGE_NO_MAIN_ACTIVITY);
		MainController.initRedDot(mRedDotView);
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}

	@OnClick({R.id.title_left_iv, R.id.title_right_iv, R.id.pre_increase_policy_iv, R.id.view_more_policy})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.title_left_iv:
				EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_HELP_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
				break;
			case R.id.title_right_iv:
				EventAgent.post(new QuotaEvent.PageJumpEvent(QuotaEvent.PageJumpEvent.QUOTA_JUMP_MESSAGE_PAGE, Constants.PAGE_NO_MAIN_ACTIVITY));
				MainController.hideRedDot(mRedDotView);
				break;
			case R.id.pre_increase_policy_iv:
				StatService.onEvent(getActivity(), StatIDSet.EVENT_ID_FORCED_INCREASE_INCREASE_BY_POLICY);
				IncreaseLimitByPolicyActivity.start(getActivity(), false);
				break;
			case R.id.view_more_policy:
				OutletAppointActivity.start(getActivity(), mApplyStatus);
				break;
		}
	}

	@Override
	public void onFunctionalCompleted(boolean autoTips) {
		super.onFunctionalCompleted(autoTips);
		dismissLoading();
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onRedDotShowEvent(QuotaEvent.ShowRedDotEvent event) {
		mRedDotView.setVisibility(View.VISIBLE);
	}
}
